package com.jqr.activity.mycoroutine

import android.annotation.SuppressLint
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.jqr.activity.mycoroutine.api.userServiceApi

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity2 : AppCompatActivity() {
    @OptIn(DelicateCoroutinesApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main1)

        val nameTextView=findViewById<TextView>(R.id.textView);
        nameTextView.text="hello world"

        val  btnClick=findViewById<Button>(R.id.btnClick).also {

            it.setOnClickListener{
                //启动协程
                //Dispatchers.Main 主线程
                //Dispatchers.IO 子线程
                // 协程的挂起和恢复
                GlobalScope.launch (Dispatchers.Main){
                    val articles=  withContext(Dispatchers.IO){
                       userServiceApi.getAllArticles()
                    }

                    nameTextView.text=articles.msg
                }

            }
        }
    }
}
data class  User(val name:String,val address:String)

private suspend fun getUser(){
    val user=get();
    show(user)
}

fun show(user: User) {

}

fun get(): User{
    return User("","")
}
